#include "Socket.hpp"
#include "protocol.hpp"
#include <iostream>
#include <string>

int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        std::cerr << "Usage: " << argv[0] << " server_port server_port" << std::endl;
        exit((int)exit_code::USAGE_ERR);
    }

    std::string server_ip = argv[1];
    uint16_t server_port = std::stoi(argv[2]);

    std::shared_ptr<Socket> sock = std::make_shared<TcpSocket>();
    sock->BuildTcpClientListenSocket();

    if(sock->Connect(server_ip, server_port) < 0)
    {
        std::cerr << "connect error" << std::endl;
        exit((int)exit_code::CONNECT_ERR);
    }

    std::unique_ptr<Protocol> protocol = std::make_unique<Protocol>();

    std::string buff;
    while(true)
    {
        int x, y;
        char op;
        std::cout << "Inter x: ";
        std::cin >> x;
        std::cout << "Inter y: ";
        std::cin >> y;
        std::cout << "Inter op: ";
        std::cin >> op;
        
        //构建请求
        std::string req_str = protocol->BuildRequest(x, y, op);

        //发送请求
        sock->Send(req_str);

        //获取应答
        Response rep;
        if(!protocol->GetResponse(sock, buff, rep)) break;

        //显示结果
        rep.ShowResult();
    }
    return 0;
}